import Cookie from 'js-cookie'
export const state = () => ({
  contentReplyData: [],
  contentUserReplyData: [],
  pageSize: 10,
  pageNum: 0,
  currentContent: {},
  currentReply: {},
  replyModal: false,
  replyContent: '',
  commentContent: '',
  commentUserData: [],
  commentModal: false,
  moreCommentModal: false,
  contentLoading: '4' // 1 加载更多 2 正在加载 3 没有更多了 4 初始化显示
})

export const mutations = {
  SET_REPLY_CONTENT: function (state, replyContent) {
    state.replyContent = replyContent
  },
  SET_COMMENT_USER_DATA: function (state, commentUserData) {
    let result = state.commentUserData
    result.unshift(commentUserData)
    state.commentUserData = result
  },
  SET_CURRENT_REPLY: function (state, currentReply) {
    state.currentReply = currentReply
  },
  SET_COMMENT_CONTENT: function (state, commentContent) {
    state.commentContent = commentContent
  },
  SET_COMMENT_MODAL: function (state, commentModal) {
    state.commentModal = commentModal
  },
  SET_MORE_COMMENT_MODAL: function (state, moreCommentModal) {
    state.moreCommentModal = moreCommentModal
  },
  SET_REPLY_MODAL: function (state, replyModal) {
    state.replyModal = replyModal
  },
  SET_CURRENT_CONTENT: function (state, currentContent) {
    state.currentContent = currentContent
  },
  SET_CONTENT_REPLY_DATA: function (state, contentReplyData) {
    state.contentReplyData = contentReplyData
  },
  SET_CONTENT_USER_REPLY_DATA: function (state, contentReplyData) {
    let result = state.contentReplyData
    result.unshift(contentReplyData)
    state.contentReplyData = result
  },
  SET_KEYWORDS: function (state, keywords) {
    state.keywords = keywords
  },
  SET_CONTENT_LOADING: function (state, contentLoading) {
    state.contentLoading = contentLoading
  },
  SET_PAGE_NUM: function (state, pageNum) {
    state.pageNum = pageNum
  }
}

export const actions = {
  // oper 判断 请求的方式 是 浏览器回退、点击加载更多、访问url初始化数据
  getContentReplyData ({commit}, {oper, contentId}) {
    let that = this
    let pageNum = that.state.content_detail.pageNum
    // 点击分页
    if (oper === 'more') {
      pageNum = pageNum + 1
      commit('SET_PAGE_NUM', pageNum)
    } else {
      pageNum = 1
      commit('SET_PAGE_NUM', pageNum)
    }
    // 改变为加载状态
    commit('SET_CONTENT_LOADING', '2')
    let result = that.state.content_detail.contentReplyData
    that.$axios.get('/content/detail/' + contentId + '/' + that.state.content_detail.pageSize + '/' + that.state.content_detail.pageNum, {}).then(function (data) {
      let _d = data.data.list
      if (_d === null || _d === undefined) {
        _d = []
      }
      if (result === null || result === undefined) {
        result = []
      }
      if (oper === 'more') {
        result = result.concat(_d)
      } else {
        result = _d
      }
      commit('SET_CONTENT_REPLY_DATA', result)
      let _loading = '1'
      if (data.data.isLastPage && data.data.total > 0) {
        _loading = '3'
      }
      if (data.data.isLastPage && data.data.total === 0) {
        _loading = '4'
      }
      commit('SET_CONTENT_LOADING', _loading)
    }).catch(function (err) {
      console.log(err)
    })
  },
  doReply ({commit}) {
    let that = this
    let _reply = {
      contentId: that.state.content_detail.currentContent.id,
      author: JSON.parse(Cookie.get('user')).id,
      content: that.state.content_detail.replyContent,
      cContent: {
        id: that.state.content_detail.currentContent.id,
        replyCount: (that.state.content_detail.currentContent.replyCount + 1)
      }
    }
    that.$axios.post('/reply/add', _reply).then(function (data) {
      commit('SET_CONTENT_USER_REPLY_DATA', data.data)
    }).catch(function (err) {
      console.log(err)
    })
  },
  doComment ({commit}) {
    let that = this
    let comment = {
      replyId: that.state.content_detail.currentReply.id,
      content: that.state.content_detail.commentContent,
      author: JSON.parse(Cookie.get('user')).id,
      reply: {
        id: that.state.content_detail.currentReply.id,
        commentCount: (that.state.content_detail.currentReply.commentCount + 1)
      }
    }
    that.$axios.post('/replyComment/add', comment).then(function (data) {
      commit('SET_COMMENT_USER_DATA', data.data)
    }).catch(function (err) {
      console.log(err)
    })
  },
  setCurrentContent ({commit}, {contentId}) {
    this.$axios.get('/content/get/' + contentId, {}).then(function (data) {
      commit('SET_CURRENT_CONTENT', data.data)
    }).catch(function (err) {
      console.log(err)
    })
  },
  setReplyModal ({commit}, {replyModal}) {
    commit('SET_REPLY_MODAL', replyModal)
  },
  setCommentModal ({commit}, {commentModal}) {
    commit('SET_COMMENT_MODAL', commentModal)
  },
  setReplyContent ({commit}, {replyContent}) {
    commit('SET_REPLY_CONTENT', replyContent)
  },
  setCommentContent ({commit}, {commentContent}) {
    commit('SET_COMMENT_CONTENT', commentContent)
  },
  setCurrentReply ({commit}, {currentReply}) {
    commit('SET_CURRENT_REPLY', currentReply)
  },
  setCommentUserData ({commit}, {commentUserData}) {
    commit('SET_COMMENT_USER_DATA', commentUserData)
  },
  setMoreCommentModal ({commit}, {moreCommentModal}) {
    commit('SET_MORE_COMMENT_MODAL', moreCommentModal)
  }
}
